package Source;

import org.apache.flink.streaming.api.functions.source.SourceFunction;
import transformation.Access;

import java.util.Random;

/**
 * Create by zhh on 2022/9/6.
 */
public class AccessSource implements SourceFunction<Access> {
    boolean running=true;
    @Override
    public void run(SourceContext<Access> sourceContext) throws Exception {
        String[] domains={"cai.com","baidu.com","b.com"};
        Random random = new Random();
        while (running){
            for (int i = 0; i < 10; i++) {
                Access access = new Access();
                access.setTime(1234L);
                access.setDomain(domains[random.nextInt(domains.length)]);
                access.setTraffic(random.nextDouble()+1000);
                sourceContext.collect(access);
            }
            Thread.sleep(5000);
        }
    }

    @Override
    public void cancel() {
        running=false;
    }
}
